package com.dfdchem.imd.kaiwu.model;

import android.content.Context;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.facade.service.SerializationService;
import com.dfdchem.imd.kaiwu.util.JSON;
import com.dfdchem.imd.kaiwu.util.Log;

import java.lang.reflect.Type;

@Route(path = "/service/json")
public class JsonServiceImpl implements SerializationService {

    private static final String TAG = "JsonServiceImpl";

    @Override
    public <T> T json2Object(String input, Class<T> clazz) {
        return JSON.parseObject(input, clazz);
    }

    @Override
    public String object2Json(Object instance) {
        return JSON.toJSONString(instance);
    }

    @Override
    public <T> T parseObject(String input, Type clazz) {
        try {
            return com.alibaba.fastjson.JSON.parseObject(input, clazz);
        } catch (Exception e) {
            Log.e(TAG, "parseObject " + "json \n" + input + " catch \n" + e.getMessage());
        }
        return null;
    }

    @Override
    public void init(Context context) {

    }
}
